//package com.plian.system.service.pm.transfer.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.fasterxml.jackson.core.type.TypeReference;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import com.google.gson.GsonBuilder;
//import com.itextpdf.text.pdf.AcroFields;
//import com.itextpdf.text.pdf.BaseFont;
//import com.itextpdf.text.pdf.PdfReader;
//import com.itextpdf.text.pdf.PdfStamper;
//import com.plian.Tools.CollectionUtil;
//import com.plian.Tools.DateUtil;
//import com.plian.Tools.Func;
//import com.plian.Tools.StringPool;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.common.status.MyCustomCode;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dao.bt.baseinfo.BaseInfoDao;
//import com.plian.system.entity.pm.transfer.TransferExamine;
//import com.plian.system.mapper.pm.transfer.TransferExamineMapper;
//import com.plian.system.model.CommonCode;
//import com.plian.system.model.ObjResult;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.mybatis.support.Condition;
//import com.plian.system.pdf.OutPdf;
//import com.plian.system.service.pm.transfer.ITransferExamineService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.vo.pm.transfer.TransferExamineVO;
//import com.plian.system.wrapper.pm.transfer.TransferExamineWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.io.FileUtils;
//import org.apache.commons.io.output.ByteArrayOutputStream;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.springframework.core.io.DefaultResourceLoader;
//import org.springframework.core.io.ResourceLoader;
//import org.springframework.stereotype.Service;
//import org.springframework.util.StringUtils;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.OutputStream;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//import static com.plian.system.constant.sys.FormTypeConstant.CQZC_TRANSFER_EXAMINE;
//
///**
// * 服务实现类
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class TransferExamineServiceImpl extends BaseServiceImpl<TransferExamineMapper, TransferExamine> implements ITransferExamineService, BaseFlowService {
//
//    private  TransferExamineMapper transferExamineMapper;
//
//    private IFormCodeService formCodeService;
//
//    private BaseInfoDao baseInfoDao;
//
//    private TransferExamineWrapper transferExamineWrapper;
//
//    @Override
//    public boolean saveOrUpdate(TransferExamine transferExamine) {
//        if (!Optional.ofNullable(transferExamine.getId()).isPresent()) {
//            transferExamine.setVersion("1");
//            transferExamine.setCode(formCodeService.getCode(FormTypeConstant.CQZC_TRANSFER_EXAMINE));
//            transferExamine.setFormType(String.valueOf(FormTypeConstant.CQZC_TRANSFER_EXAMINE));
//        }
//        if (Optional.ofNullable(transferExamine.getStatus()).isPresent() && transferExamine.getStatus() != -1) {
//            transferExamine.setStatus(transferExamine.getStatus());
//        }
//        return super.saveOrUpdate(transferExamine);
//    }
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//        for (Long id : ids) {
//            TransferExamine transferExamine = getById(id);
//            if (transferExamine == null) {
//                return false;
//            } else if (transferExamine.getStatus() != null &&
//                    (transferExamine.getStatus() != FormStatusConstant.STASH && transferExamine.getStatus() != FormStatusConstant.UNREVIEWED)) {
//                return false;
//            }
//        }
//        return super.deleteLogic(ids);
//    }
//
//    @Override
//    public HashMap<String, Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//        //将需要提交的流程变量返回，用于工作流侧
//        return new HashMap<String, Object>();
//    }
//
//
//    @Override
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    @Override
//    public void goBackToStash(String formId) {
//        handleStatus(Long.valueOf(formId), UNREVIEWED);
//    }
//
////    @Override TODO DEL
////    public ListResult<PageData> reportList(PageData pd) {
////        Page page=new Page(false,pd);
////        try {
////            List<PageData> list= transferExamineMapper.reportListPage(page);
////            ListResult listResult=new ListResult(CommonCode.SUCCESS);
////            listResult.setList(list);
////            listResult.setTotal(page.getTotal());
////            return listResult;
////        } catch (Exception e) {
////            log.error(ExceptionUtils.getFullStackTrace(e));
////            throw new MyRuntimeException();
////        }
////    }
//
//    @Override
//    public String checkIsSame(String id, String code) {
//        String result = "";
//        if (Optional.ofNullable(code).isPresent()) {
//            TransferExamine transferExamine = new TransferExamine();
//            transferExamine.setCode(code);
//            if (Optional.ofNullable(getOne(Condition.getQueryWrapper(transferExamine))).isPresent()) {
//                if (!getOne(Condition.getQueryWrapper(transferExamine)).getId().equals(id)) {
//                    result = "单据编码已存在";
//                }
//            }
//        }
//        return result;
//    }
//
//    @Override
//    public Boolean checkIsExisted(Long id, String code) {
//        if (Optional.ofNullable(code).isPresent()) {
//            TransferExamine transferExamine = new TransferExamine();
//            transferExamine.setCode(code);
//            if (Optional.ofNullable(getOne(Condition.getQueryWrapper(transferExamine))).isPresent()) {
//                return id != null && id.equals(getOne(Condition.getQueryWrapper(transferExamine)).getId());
//            }
//        }
//        return true;
//    }
//
//    @Override
//    public ObjResult change(TransferExamine transferExamine) {
//        ObjResult result = checkSubmit(transferExamine);
//        if (Optional.ofNullable(result).isPresent()) {
//            return result;
//        }
//        transferExamine.setVersion(!StringUtils.hasText(transferExamine.getVersion()) ?
//                "1" : String.valueOf(Integer.valueOf(transferExamine.getVersion()) + 1));
//
//        return saveOrUpdate(transferExamine) ? new ObjResult(CommonCode.SUCCESS) : new ObjResult(CommonCode.FAIL);
//    }
//
//    @Override
//    public ObjResult checkSubmit(TransferExamine transferExamine) {
//
//        if (!checkIsExisted(transferExamine.getId(), transferExamine.getCode())) {
//            return new ObjResult(MyCustomCode.BILL_ALREADY_EXISTS2);
//        }
//        return null;
//    }
//
//    @Override
//    public List<PageData> findByIds(List<String> formIds) {
//        LambdaQueryWrapper<TransferExamine> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.in(TransferExamine::getId, Func.toLongList(org.apache.commons.lang3.StringUtils.join(formIds, StringPool.COMMA)));
//        List<TransferExamine> transferExamine = list(queryWrapper);
//        List<TransferExamineVO> transferExamineVOS = transferExamineWrapper.entityToVO(transferExamine);
//        List<PageData> pageDataList = new ArrayList<>();
//        try {
//            if (CollectionUtil.isNotEmpty(transferExamineVOS)) {
//                for (TransferExamineVO vo : transferExamineVOS) {
//                    vo.setUNITNAME(vo.getReportCompanyName());
//                    vo.setAPPLICANTNAME(vo.getCreateUserName());
//                    PageData pd = new PageData();
//                    pd.put("orgName",vo.getReportCompanyName());
//                    ObjectMapper mapper = new ObjectMapper();
//                    String json = new GsonBuilder().setDateFormat(DateUtil.PATTERN_DATETIME).create().toJson(vo);
//                    pd.putAll(mapper.readValue(json, new TypeReference<HashMap<String, String>>() {
//                    }));
//                    pageDataList.add(pd);
//                }
//            }
//            return pageDataList;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return pageDataList;
//    }
//
//    @Override
//    public List<Integer> getFormType() {
//        return Collections.singletonList(CQZC_TRANSFER_EXAMINE);
//    }
//
//    @Override
//    public File buildpdf(Long id) {
//        File file = null ;
//        try {
//            TransferExamine transferExamine = getById(id);
//            if (transferExamine!=null && !"".equals(transferExamine)) {
//                TransferExamineVO transferExamineVO = transferExamineWrapper.entityToVO(transferExamine);
//                String OrganizationalTradingMethod =transferExamineVO.getOrganizationalTradingMethod();
//                transferExamineVO.setOrganizationalTradingMethod(baseInfoDao.findbycode(OrganizationalTradingMethod).getString("name"));
//                Map< String,Object > data = new HashMap< String,Object >();
//                data.putAll(OutPdf.beanToMap(transferExamineVO));
//                ResourceLoader resourceLoader = new DefaultResourceLoader();
//                File model = File.createTempFile("logo", ".pdf");
//                FileUtils.copyInputStreamToFile(resourceLoader.getResource("/pdf/transfer_examine.pdf").getInputStream(), model);
//                String fileName = model.getPath();
//                PdfReader reader = new PdfReader(fileName);
//                ByteArrayOutputStream bos = new ByteArrayOutputStream();
//				/* 将要生成的目标PDF文件名称 */
//                PdfStamper ps = new PdfStamper(reader, bos);
//				/* 使用中文字体 */
//                BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//                ArrayList < BaseFont > fontList = new ArrayList < BaseFont > ();
//                fontList.add(bf);
//
//				/* 取出报表模板中的所有字段 */
//                AcroFields fields = ps.getAcroFields();
//                fields.setSubstitutionFonts(fontList);
//                for (String key: data.keySet()) {
//                    String value = data.get(key).toString();
//                    if(data.get(key) instanceof Date){
//                        value=processDate((Date)data.get(key));
//                    }
//                    fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
//                }
//
//                ps.setFormFlattening(true);
//                ps.close();
//                File dir = new File(model.getParent());
//                file = File.createTempFile("transfer_examine", ".pdf", dir);
//                OutputStream fos = new FileOutputStream(file);
//                fos.write(bos.toByteArray());
//                fos.flush();
//                fos.close();
//                bos.close();
//            }
//
//        } catch(Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//        }
//        return file;
//    }
//
//    private static String processDate(Date date){
//        SimpleDateFormat formatter=new SimpleDateFormat("yyyy/MM/dd");
//        return date==null?"":formatter.format(date);
//    }
//
//    /**
//     * @param id           主键
//     * @param updateStatus 状态
//     * @return
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            TransferExamine transferExamine = getById(id);
//            transferExamine.setStatus(updateStatus);
//            updateById(transferExamine);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//}
